package test.administrator.example.com.mvptest1.framework.lce.impl;

import android.view.View;

import test.administrator.example.com.mvptest1.R;
import test.administrator.example.com.mvptest1.framework.lce.ILceAnimator;
import test.administrator.example.com.mvptest1.framework.lce.MvpLceView;
import test.administrator.example.com.mvptest1.framework.lce.impl.animator.DefaultLceAnimator;

/**
 * Created by thinkpad on 2018/7/30.
 */

public class MvpLceViewImpl<D> implements MvpLceView<D> {

	//处理动画
	private ILceAnimator animator;

	public void setAnimator(ILceAnimator animator) {
		this.animator = animator;
	}

	private ILceAnimator getAnimator() {
		if (this.animator == null) {
			this.animator = new DefaultLceAnimator();
		}
		return animator;
	}

	//加载View
	private View loadingView;
	private View contentView;
	private View errorView;

	public void initView(View rootView) {
		if (rootView == null) {
			throw new NullPointerException("root view 不能为空");
		}
		if (loadingView == null) {
			this.loadingView = rootView.findViewById(R.id.loadingView);
		}
		if (contentView == null) {
			this.contentView = rootView.findViewById(R.id.contentView);
		}
		if (errorView == null) {
			this.errorView = rootView.findViewById(R.id.errorView);
		}
		if (loadingView == null) {
			throw new NullPointerException("loadingView view 不能为空");
		}
		if (contentView == null) {
			throw new NullPointerException("contentView view 不能为空");
		}
		if (errorView == null) {
			throw new NullPointerException("errorView view 不能为空");
		}
	}

	public void setOnClickErrorListener(View.OnClickListener onClickListener) {
		if (this.errorView != null) {
			this.errorView.setOnClickListener(onClickListener);
		}
	}

	@Override
	public void loadData(boolean isPullRefresh) {

	}

	@Override
	public void showLoading(boolean isPullToRefresh) {
		if (!isPullToRefresh) {
			getAnimator().showLoadingView(loadingView, contentView, errorView);
		}
	}

	@Override
	public void showContent() {
		getAnimator().showContentView(loadingView, contentView, errorView);
	}

	@Override
	public void bindData(D data) {

	}

	@Override
	public void showError(Throwable e, boolean isPullToRefresh) {
		if (!isPullToRefresh) {
			getAnimator().showErrorView(loadingView, contentView, errorView);
		}
	}

}
